package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "User", description = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController{
  @Autowired
  private UserService userService;

  /**
   * 登陆
   * @param para
   * @return
   * @throws JsonProcessingException
   */
  @ApiOperation("登录")
  @RequestMapping(value = "/login",method = RequestMethod.POST)
  public String login(@RequestBody Map<String,Object> para) throws JsonProcessingException {
    String username=(String)para.get("username");
    String password=(String)para.get("password");
    User user = userService.login(username,password);
    HashMap<String,Object> hs=new HashMap<>();
    if(user!=null){
      String  token = TokenUtil.sign(user);
      hs.put("token",token);
      hs.put("data",user);
      hs.put("meta",new Constance("msg",200));
    }else {
      hs.put("meta",new Constance("请重新输入用户名密码",404));
    }
    ObjectMapper objectMapper=new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }

  /**
   * 查询所有用户 及相关信息
   * @return
   * @throws JsonProcessingException
   */
  @ApiOperation("获取查询所有用户及相关信息")
  @RequestMapping(value = "/his_user",method = RequestMethod.POST)
  public String listUser(@RequestBody Map<String,Object> para) throws JsonProcessingException {

    System.out.println(para);

    // 接受前端传过来的，起始页，每页记录数这两个参数，将其转换为整数
    Integer startPage= (Integer) para.get("pagenum");
    Integer pageSize= (Integer) para.get("pagesize");

    // 创建Page对象，将page，limit参数传入，必须位于从数据库查询数据的语句之前，否则不生效
    Page page= PageHelper.startPage(startPage, pageSize);

    //  ASC是根据id 正向排序，DESC是反向排序
    PageHelper.orderBy("id ASC");

    //调取方法查询数据库
    List LUser = userService.listUser();

    // 获取查询记录总数，必须位于从数据库查询数据的语句之后，否则不生效
    long total = page.getTotal();

    HashMap<String, Object> hs = new HashMap<>();
    if (LUser != null) {
      hs.put("data",LUser);
      hs.put("count",total);
      hs.put("meta", new Constance("msg", 200));
    } else {
      hs.put("meta", new Constance("msg", 404));
    }
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }
  @ApiOperation("根据搜索条件获取用户数据")
  @RequestMapping(value = "/selectBySS",method = RequestMethod.POST)
  public String selectBySS(@RequestBody User user) throws JsonProcessingException {
    //Map<String,Object> para
    //检索是否从前台获得数据
    System.out.println(user);


    Integer startPage =user.getPagenum();
    Integer pageSize= user.getPagesize();

    //把获取到的搜索框数据同时赋值给用户名，实现用户名登录名同时查询
    user.setUsername(user.getRealname());

    // 创建Page对象，将page，limit参数传入，必须位于从数据库查询数据的语句之前，否则不生效
    Page page= PageHelper.startPage(startPage, pageSize);
    //  ASC是根据id 正向排序，DESC是反向排序
//    PageHelper.orderBy("id ASC");

    List<User> userList = userService.selectBySS(user);

    // 获取查询记录总数，必须位于从数据库查询数据的语句之后，否则不生效
    long total = page.getTotal();

    HashMap<String,Object> hs = new HashMap<>();
    if (userList != null) {
      hs.put("data",userList);
      hs.put("count",total);
      hs.put("pagenum",startPage);
      hs.put("meta", new Constance("msg", 200));
    } else {
      hs.put("meta", new Constance("msg", 404));
    }
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }

  /**
   * 添加用户
   * @param user
   * @return
   * @throws JsonProcessingException
   */
  @RequestMapping(value = "/addUser",method = RequestMethod.POST)
  public String addUser(@RequestBody User user) throws JsonProcessingException {

    //挂号级别，删除数值
    user.setRegistleid(2);
    user.setDelmark(1);

    HashMap<String,Object> hs = new HashMap<>();
    Integer u = userService.insertSelective(user);
    if (u == 1) {
      hs.put("data",u);
      hs.put("meta", new Constance("msg", 200));
    } else {
      hs.put("meta", new Constance("msg", 404));
    }

    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }

  /**
   * 根据id查询用户
   * @param editForm
   * @return
   * @throws JsonProcessingException
   */
  @RequestMapping(value = "/showEditUser",method = RequestMethod.POST)
  public String selectByID(@RequestBody User editForm) throws JsonProcessingException {

    Integer i = editForm.getId();
    HashMap<String,Object> hs = new HashMap<>();
    User user = userService.selectByPrimaryKey(i);
    if (user != null) {
      hs.put("data",user);
      hs.put("meta", new Constance("msg", 200));
    } else {
      hs.put("meta", new Constance("msg", 404));
    }
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }

  /**
   * 修改用户
   * @param user
   * @return
   * @throws JsonProcessingException
   */
  @RequestMapping(value = "/editUser",method = RequestMethod.POST)
  public String editUser(@RequestBody User user) throws JsonProcessingException {

    HashMap<String,Object> hs = new HashMap<>();
    Integer u = userService.updateByPrimaryKeySelective(user);
    if (u == 1) {
      hs.put("data",u);
      hs.put("meta", new Constance("msg", 200));
    } else {
      hs.put("meta", new Constance("msg", 404));
    }

    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }

  // 删除用户（改变delmark值来完成逻辑删除）
  @RequestMapping(value = "/removeUser",method = RequestMethod.POST)
  public String removeUser(@RequestBody User remove) throws JsonProcessingException {

    Integer id = remove.getId();
    User removeUser = new User();
    removeUser.setDelmark(0);
    removeUser.setId(id);

    HashMap<String,Object> hs = new HashMap<>();
    Integer u = userService.removeUser(removeUser);
    if (u == 1) {
      hs.put("data",u);
      hs.put("meta", new Constance("msg", 200));
    } else {
      hs.put("meta", new Constance("msg", 404));
    }

    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }



  @RequestMapping(value = "/test",method = RequestMethod.POST)
  @ResponseBody
  public String test(@RequestBody Map<String,Object> para) throws JsonProcessingException {
    HashMap<String,Object> hs=new HashMap<>();
    hs.put("data","data");
    ObjectMapper objectMapper=new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }



}
